package com.application.DataProvider;

import com.application.DTO.AccountDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class AccountDAO{

	Context _mcontext = null;

	public AccountDAO(Context context){
		this._mcontext = context;
	}

	public boolean isExist(String userName)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Account where UserName= '" +  userName+"'";
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}

	public boolean isExist(String userName, String pass)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Account where UserName= '" +  userName+"' and password='"+pass+"'";
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	
	public String getPass(String acc)
	{
		String pass = null;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select password from Account where UserName = '" + acc +"'";
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				pass = cur.getString(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return pass;
	}
	
//	public int getAvarta(String acc)
//	{
//		int avarta = 0;
//		DataProvider provider = new DataProvider(_mcontext);
//		SQLiteDatabase db = provider.openDataBase();
//		Cursor cur = null;
//		try 
//		{
//			String sql = "Select Avarta from Learner where UserName = '" + acc +"'";
//			cur = provider.execQuery(db, sql);
//			while (cur.moveToNext()) {
//				avarta = cur.getInt(0);
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//		} finally{
//			cur.close();
//			db.close();
//		}
//
//		return avarta;
//	}
	
	//Insert du lieu vao bang Account
	public int insertAccount(AccountDTO aDto) {
		Log.d("PEERDROID","insert chap");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("UserName", aDto.get_Username());
			values.put("Password", aDto.get_Password());
			values.put("Role", aDto.get_Role());

			result = provider.execInsertCommand(db, "Account", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
	
	public int insertAccount(String userName, String pass) {
		Log.d("PEERDROID","insert account");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("UserName", userName);
			values.put("Password", pass);
			values.put("Role", 0);

			result = provider.execInsertCommand(db, "Account", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Cap nhat bang Account
	public int updateAccount(AccountDTO aDto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Password", aDto.get_Password());
			values.put("Role", aDto.get_Role());

			String whereClause="UserName=?";
			String[]whereArgs = {aDto.get_Username()};
			result= provider.execUpdateCommand(db, "Account", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}

		return result;
	}

	//Delete doi tuong AccountDTO
	public int deleteAccount(String userName)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="UserName=?";
			String[]whereArgs = {String.valueOf(userName)};	
			result=provider.execDeleteCommand(db, "Account", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			db.close();
		}
		return result;
	}
}
